﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sunny.Lib
{
    public enum RecordOperation
    {
        Add,
        Modify,
        Del,
        Read,
    }

    public class UIHelper
    {
        public static string ControlNamePreFix = "ctrl_";

        public static void BindDataToPanel<T>(T record, Panel panel)
        {
            if (record != null)
            {
                Type type = record.GetType();
                System.Reflection.PropertyInfo[] properties = type.GetProperties();
                foreach (var property in properties)
                {
                    Control[] controls = panel.Controls.Find(ControlNamePreFix + property.Name, true);
                    if (controls != null && controls.Count() > 0)
                    {
                        if (controls[0] is TextBox)
                        {
                            controls[0].Text = property.GetValue(record, null).ToString();
                        }
                    }
                }
            }
        }

        public static T GetDataFromPanel<T>(Panel panel) where T : new()
        {
            Type type = typeof(T);
            T record = new T();
            System.Reflection.PropertyInfo[] properties = type.GetProperties();
            foreach (var property in properties)
            {
                Control[] controls = panel.Controls.Find(ControlNamePreFix + property.Name, true);
                if (controls != null && controls.Count() > 0)
                {
                    if ((controls[0] is TextBox) | controls[0] is RichTextBox)
                    {
                        property.SetValue(record, ChangeType(property.PropertyType, controls[0].Text.Trim()), null);
                    }
                }
            }

            return record;
        }

        public static object ChangeType(Type type, object value)
        {
            if ((value == null) && type.IsGenericType)
            {
                return Activator.CreateInstance(type);
            }
            if (value == null)
            {
                return null;
            }
            if (type == value.GetType())
            {
                return value;
            }
            if (type.IsEnum)
            {
                if (value is string)
                {
                    return Enum.Parse(type, value as string);
                }
                return Enum.ToObject(type, value);
            }
            if (!type.IsInterface && type.IsGenericType)
            {
                Type type1 = type.GetGenericArguments()[0];
                object obj1 = ChangeType(type1, value);
                return Activator.CreateInstance(type, new object[] { obj1 });
            }
            if ((value is string) && (type == typeof(Guid)))
            {
                return new Guid(value as string);
            }
            if ((value is string) && (type == typeof(Version)))
            {
                return new Version(value as string);
            }
            if (!(value is IConvertible))
            {
                return value;
            }
            if (value.ToString() == string.Empty)
            {
                value = null;
                return value;
            }

            return Convert.ChangeType(value, type);
        }
    }
}
